import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '@/views/Home'
import Store from '@/store'

Vue.use(VueRouter)

// 解决跳转到当前路由报错问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err)
}


const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeView,
  },
  {
    path:'/products',
    component:()=>import(/* webpackChunkName: "products" */'@/views/AllProduct')
  },
  {
    path:"/user",
    component:()=>import(/* webpackChunkName: "user" */"@/views/User"),
    redirect:"/user/strategy",
    children:[{
      path:"strategy",
      component:()=>import(/* webpackChunkName: "strategy" */"@/components/strategy")
    }]
  },
  {
    path:"/detail/:id",
    name:"detail",
    component:()=>import(/* webpackChunkName: "detail" */"@/views/DetailProduct")
  }
  // {
  //   path: '/about',
  //   name: 'about',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  // }
]


const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

//路由守卫
router.beforeEach((to,from,next)=>{
  if(/^\/user/.test(to.path)){
    if(!localStorage.getItem("x-auth-token")){
      // Store.commit("changeOpenLogin",true);
    }
  }
  next();
})
export default router
